package com.etone.smartAudit.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etone.smartAudit.domain.ItemMember;
import com.etone.smartAudit.mapper.ItemMemberMapper;
import com.etone.smartAudit.vo.item.ItemMemberVO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author: sovy
 * @date: 2020/11/9
 * @description:
 */

@Service
public class ItemMemberService extends ServiceImpl<ItemMemberMapper, ItemMember> {

    /**
     * 更新项目成员
     *
     * @param itemId
     * @param itemMembers
     */
    public void updateItemMember(int itemId, List<ItemMember> itemMembers) {
        //删除旧项目成员
        deleteByItemId(itemId);

        //增加新的项目成员
        if (!CollectionUtils.isEmpty(itemMembers)) {
            itemMembers.stream().forEach(itemMember -> itemMember.setItemId(itemId));
            saveBatch(itemMembers);
        }
    }

    /**
     * 查找项目成员
     *
     * @param itemId
     * @return
     */
    public List<ItemMemberVO> findByItemId(int itemId) {
        return baseMapper.findByItemId(itemId);
    }

    /**
     * 通过项目id删除项目成员
     *
     * @param itemId
     */
    public void deleteByItemId(int itemId) {
        baseMapper.deleteByItemId(itemId);
    }

}

